#include "equation.h"

void StartMenu(){
    std::cout << "====================================================================" << std::endl;
    std::cout << "Equation resolving start." << std::endl;
    std::cout << "Enter \'q\' or \'Q\' if you want to quit or \'Y\'(y) to continue." << std::endl;
    while(true){
        char opt = getchar();
        if('q'==opt || 'Q'==opt){
            std::cout << "programer end." << std::endl;
            exit(0);
        } else if ('y'==opt || 'Y'==opt){
            std::cout << "please enter the arguments of your equation:" << std::endl;
            break;
        } else {
            std::cout << "enter error, please enter the option again." << std::endl;
            continue;
        }
    }
}

bool MidMenu(){
    std::cout << "====================================================================" << std::endl;
    std::cout << "Enter \'q\' or \'Q\' if you want to quit or \'Y\'(y) to continue." << std::endl;
    while(true){
        // std::cout << "Enter \'q\' or \'Q\' if you want to quit or \'Y\'(y) to continue." << std::endl;
        char opt = getchar();
        if('q'==opt || 'Q'==opt){
            std::cout << "programer end." << std::endl;
            return false;
        } else if ('y'==opt || 'Y'==opt){
            std::cout << "please enter the arguments of your equation:" << std::endl;
            return true;
        } else {
            std::cout << "enter error, please enter the option again." << std::endl;
            continue;
        }
    }
}

void End(){
    std::cout << "====================================================================" << std::endl;
    std::cout << "resolving end.\nprogram exit." << std::endl;
    return;
}

int Resolve(){
    double a = 0;
    double b = 0;
    double c = 0;
    GetArgs(a, b, c);
    double delta = b*b - 4*a*c;

    double mem_1 = -b/(2 * a);
    double mem_2 =sqrt(abs(delta))/(2 * a);

    if(delta < 0){
        std::cout << "this equation has no real solution" << std::endl;
        std::cout << "the two complex solutions are:" << std::endl;
        std::cout << mem_1 << "+i" << mem_2 << '\t' << mem_1 << "-i" << mem_2 << std::endl;
        return 0;
    } else if (0==delta){
        std::cout << "this equation has one real solution:" << std::endl;
        std::cout << "x_1 = " << mem_1 << std::endl;
        return 1;
    } else {
        std::cout << "this equation has two real solutions:" << std::endl;
        std::cout << "x_1 = " << mem_1+mem_2 << "\tx_2 = " << mem_1-mem_2 << std::endl;
        return 2;
    }
}

void GetArgs(double & a, double & b, double & c){
    std::cin >> a;
    std::cin >> b;
    std::cin >> c;
    return ;
}